package com.sctvcloud.qingchuan.ui.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Cache;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.base.BaseActivity;
import com.sctvcloud.qingchuan.beans.PoliticsReplyDotBean;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.adapter.PoliticsDetailReplyListAdapter;
import com.sctvcloud.qingchuan.ui.adapter.holder.PoliticsUserReplyHolder;
import com.sctvcloud.qingchuan.ui.dialog.PoliticsReplyDiaFragment;
import com.sctvcloud.qingchuan.ui.utils.GlideCircleTransform;
import com.sctvcloud.qingchuan.ui.utils.GlideUtil;
import com.sctvcloud.qingchuan.ui.utils.TitleUtils;
import com.sctvcloud.qingchuan.utils.UserManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 更多回复（回复详情）
 */
public class PoliticsMoreReplyActivity extends BaseActivity {
    @BindView(R.id.author_reply_content)
    protected CustomFontTextView author_reply_content;
    @BindView(R.id.reply_time)
    protected CustomFontTextView reply_time;
    @BindView(R.id.author_name)
    protected CustomFontTextView author_name;
    @BindView(R.id.author_avatar)
    protected ImageView author_avatar;


    @BindView(R.id.replay_layout)
    protected LinearLayout replay_layout;
    @BindView(R.id.user_reply_list)
    protected RecyclerView user_reply_list;

    protected PoliticsReplyDiaFragment commentDiaFragment;
    public static final String POLITICS_REPLY_ID = "politics_reply_id";
    public static final String POLITICS_ID = "politics_id";
    public static final String POLITICS_USER_ID = "politics_user_id";
    private PoliticsUserReplyHolder.OnReplyClickListener replyClickListener;
    private PoliticsDetailReplyListAdapter questioningAdapter;
    private PoliticsReplyDotBean replyDetail;

    private long replyId = 0;
    private int interactionId = 0;
    private long politicsUserId = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("回复详情");
        interactionId = getIntent().getIntExtra(POLITICS_ID, 0);
        replyId = getIntent().getLongExtra(POLITICS_REPLY_ID, 0);
        politicsUserId = getIntent().getLongExtra(POLITICS_USER_ID, 0);

        initView();
        getData();
    }

    @Override
    protected void onDestroy() {
        Cache.getInstance().setPcCondition(null);
        super.onDestroy();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_politics_more_reply);
        ButterKnife.bind(this);
    }


    private void initView() {
        user_reply_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        user_reply_list.setItemAnimator(new DefaultItemAnimator());
        questioningAdapter = new PoliticsDetailReplyListAdapter(this, null, null);
        user_reply_list.setAdapter(questioningAdapter);
    }

    private void getData() {
        if (replyId > 0) {
            showProgress("正在加载...");
            String userId = "";
            if (null != UserManager.getInstance().getUser()) {
                userId = UserManager.getInstance().getUser().getUserId();
            }
            NetUtils.getNetAdapter().toGetMorePoliticsReply(getOwnerName(), Constances.TENANT_ID, replyId, 1, interactionId, userId, new AbsNetCallBack<String>(String.class) {
                @Override
                public void onSucStr(String news) {
                    if (!TextUtils.isEmpty(news)) {
                        try {
                            replyDetail = gson.fromJson(news, PoliticsReplyDotBean.class);
                            initData();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        onError(null, "数据异常");
                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
                    toast(errorMsg);
                }


                @Override
                public void onEnd() {
                    closeProgress();
                }
            });
        }


    }

    private void initData() {
        author_reply_content.setText(replyDetail.getContent());
        reply_time.setText(DateUtils.formatDate(DateUtils.getDataDate(replyDetail), ""));
        author_name.setText(replyDetail.getNickName());
        GlideUtil.getGlideWithAvatorDef(this, replyDetail.getAvatar()).transform(new GlideCircleTransform(this)).into(author_avatar);
        questioningAdapter.setData(replyDetail.getInteractionReplyDtoList());
    }

    @OnClick({R.id.reply})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reply:
                replyClick(replyDetail);
                break;
        }
    }

    private void replyClick(PoliticsReplyDotBean t) {
        if (UserManager.getInstance().isLogin()) {
            String userId = UserManager.getInstance().getUser().getUserId();
            long id = Long.parseLong(userId);
            if (id == politicsUserId) {
                FragmentManager fm = getSupportFragmentManager();
//        if (commentDiaFragment == null) {
                commentDiaFragment = new PoliticsReplyDiaFragment();
                commentDiaFragment.setSendComment(new PoliticsReplyDiaFragment.SendComment() {
                    @Override
                    public boolean onCommentSendPrepare(int mode) {
                        return false;
                    }

                    @Override
                    public void onCommentSendSuccess(int mode) {
                        toast("发送成功");
                        setResult(RESULT_OK);
                        getData();
                    }

                    @Override
                    public void onCommentSendFinish(int mode, boolean isNetSuc) {

                    }
                });

                commentDiaFragment.setReplyInfo(interactionId, t.getId());
                commentDiaFragment.show(fm, "dia_politics_reply");
                fm.executePendingTransactions();
            }else{
                toast("您不是提问用户，不能追问");
            }
        } else {
            toLogin();
        }
    }
}
